<?php
    use User\Model\MemberClassModel;
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                层级详情
            </header>
            <div class="panel-body">
                <form class="form-horizontal" method="post"
                      action="<?php if($type=='add'){echo '/User/Member/classAdd';}
                      else{echo '/User/Member/classUpdate';}?>" style="width: 70%;">
                    <input type="hidden" value="<?php $val = (!empty($member_class)) ?
                            $member_class['class_id'] : ''; echo $val;?>" name="classId" id="classId">
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">名称:</label>

                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" value="<?php $val=(!empty($member_class)) ?
                            $member_class['name'] : ''; echo $val;?>" id="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">费用:</label>

                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="need_pay_money" name="need_pay_money" value="<?php $val=(!empty($member_class)) ?
                            $member_class['need_pay_money'] : ''; echo $val;?>">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">支付周期:</label>

                        <div class="col-sm-10">
                            <input type="text" id="pay_cycle" class="form-control" name="pay_cycle" value="<?php $val=(!empty($member_class)) ?
                            $member_class['pay_cycle'] : ''; echo $val;?>">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">周期单位:</label>

                        <div class="col-sm-10">
                            <select class="form-control" id="pay_cycle_unit" name="pay_cycle_unit">
                                <?php
                                    foreach(MemberClassModel::getUnit() as $key=>$val_unit){
                                        echo "<option value='" . $key . "'";
                                        if(!empty($member_class) && $key == $member_class['pay_cycle_unit']){
                                            echo ' selected ';
                                        }
                                        echo ">" . $val_unit . "</option>";
                                    }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">图片:</label>
                        <div class="col-sm-10">
                        <?php
                            $img_tag = '<img width="100%" height="200px" src="" alt=" " id="classimg">';
                            if(!empty($member_class) && !empty($member_class['picurl'])){
                                $img_tag = '<img width="100%" height="200px" id="classimg" src="' . $member_class['picurl'] . '" alt=" " >';
                            }
                            $img_tag .= '<input type="file" id="picurl" >';
                            echo $img_tag;
                        ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">是否需要审核:</label>

                        <div class="col-sm-10">
                            <div class="radio"><input type="radio" class = 'msg_check' name="need_check" value="1"
                            <?php if(empty($member_class) || $member_class['need_check'] == 1){
                                echo " checked ";
                            }?>>是</div>
                            <div class="radio"><input class = 'msg_check' type="radio" name="need_check" value="2"
                            <?php if(!empty($member_class) && $member_class['need_check'] == 2){
                                echo " checked ";
                            }?>>否</div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">会员服务:</label>

                        <div class="col-sm-10">
                            <?php foreach($member_service as $val_service){ ?>
                            <input type="checkbox" class='service' value="<?php echo $val_service['service_id'];?>"
                                    <?php if(in_array($val_service['service_id'],$exist_service_ids)){
                                        echo " checked ";
                                    }?>
                                ><?php echo $val_service['name'];?>
                            <?php }?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">表单扩展信息:</label>

                        <div class="col-sm-10">
                            <?php foreach($member_property as $val_property){ ?>
                            <input type="checkbox" class='property' value="<?php echo $val_property['property_id'];?>"
                            <?php if(in_array($val_property['property_id'],$exist_property_ids)){
                                        echo " checked ";
                                    }?>
                            ><?php echo $val_property['name'];?>
                            <?php }?>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <button class="btn btn-primary" type="submit" id="submit">提交</button>
                            <button class="btn btn-default" type="button" onclick="history.back(-1);">取消</button>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
</div>
<script>
    var picdata = '';
    $(function(){
        var img_upload = document.getElementById("picurl");
        img_upload.addEventListener('change',readFile,false);
    });
    function readFile(){
        var file = this.files[0];
        if(!/image\/\w+/.test(file.type)){
            alert("请确保文件为图像类型");
            return false;
        }
        var reader=new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function() {
            var img_area = document.getElementById("classimg");
            $("#classimg").attr("src", this.result);
            picdata = this.result;
        }
    }
    $(".need").each(function(){
        $(this).prepend("<span style='color:red'>*</span>");
    });
    $("#submit").click(function(){
        var need_check = 1;
        $(".msg_check").each(function(){
            if($(this).is(':checked')){
                need_check = $(this).val();
            }
        });
        var service_ids = '';
        $(".service").each(function(){
           if($(this).is(':checked')){
               service_ids += $(this).val() + ',';
           }
        });
        var property_ids = '';
        $(".property").each(function(){
            if($(this).is(':checked')){
                property_ids += $(this).val() + ',';
            }
        });
        var data = {
            classId : $("#classId").val(),
            name : $("#name").val(),
            need_pay_money : $("need_pay_money").val(),
            pay_cycle : $("#pay_cycle").val(),
            pay_cycle_unit : $("#pay_cycle_unit").val(),
            picurl : picdata,
            need_check : need_check,
            service_ids : service_ids,
            property_ids : property_ids
        };
        var url = $("form").attr("action");
        $.ajax({
            type:'post',
            dataType:'json',
            data:data,
            url:url,
            success:function(res){
                if(res.code == 1){
                    alert("提交成功");
                    window.location = '/User/Member/memberClass';
                }else{
                    alert(res.msg);
                }
            }
        });
        return false;
    });
</script>